package com.atguigu.juc_day01.ticket_demo;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * java
 * 2022/9/6 20 10
 */

//创建资源类
class Ticket1{
    private int ticket=100;
    Lock lock = new ReentrantLock (true);
    public void sale(){
        lock.lock ();
        if (ticket>0)System.out.println (Thread.currentThread ().getName ()+"购买了后剩余"+--ticket);
        lock.unlock ();

    }
}
public class TrainTicketDemo3 {
    public static void main (String[] args) {
        Ticket1 ticket1 = new Ticket1 ();
        ReentrantLock reentrantLock = new ReentrantLock ();
        ExecutorService executorService = Executors.newFixedThreadPool (3);
        for (int i = 1; i <= 1000; i++) {
            int finalI = i;
            executorService.execute (()->{
                ticket1.sale ();
            });
        }

    }
}
